package com.to.aboomy.bannersample.viewpager;

import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.banner.OnPageItemClickListener;
import com.to.aboomy.banner.ScaleInTransformer;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.util.ArrayStringItemSelectDialog;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import com.to.aboomy.bannersample.viewpager.creator.ImageHolderCreator2;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 主页
 *
 * @since 2021-07-21
 */
public class MainAbilitySlice extends AbilitySlice {
    private Banner banner;
    private List<Integer> list = new ArrayList<>();
    private Button add;
    private Button noLoop;
    private Button remove;
    private Button update;
    private Button updateStyle;
    private int style;

    private static final String[] INDICATOR_STR = {
            "INDICATOR_CIRCLE",
            "INDICATOR_CIRCLE_RECT",
            "INDICATOR_BEZIER",
            "INDICATOR_DASH",
            "INDICATOR_BIG_CIRCLE",
    };

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_basemain);
        add = (Button) findComponentById(ResourceTable.Id_add);
        noLoop = (Button) findComponentById(ResourceTable.Id_noLoop);
        updateStyle = (Button) findComponentById(ResourceTable.Id_updateStyle);
        remove = (Button) findComponentById(ResourceTable.Id_remove);
        update = (Button) findComponentById(ResourceTable.Id_update);
        list.add(Utils.getRandom());
        list.add(Utils.getRandom());
        list.add(Utils.getRandom());
        banner = (Banner) findComponentById(ResourceTable.Id_banner);
        final IndicatorView indicatorView = new IndicatorView(this)
                .setIndicatorStyle(IndicatorView.INDICATOR_BEZIER)
                .setIndicatorRatio(1f)
                .setIndicatorRadius(2f)
                .setIndicatorSelectedRatio(3)
                .setIndicatorSelectedRadius(2f)
                .setIndicatorColor(Color.GRAY)
                .setIndicatorSelectorColor(Color.WHITE);

        banner.setIndicator(indicatorView)
                .setAutoPlay(false)
                .setPageMargin(-(AttrHelper.vp2px(88,getContext())),AttrHelper.vp2px(37,getContext()))
                .setPageTransformer(true, new ScaleInTransformer())
                .setHolderCreator(new ImageHolderCreator2())
                .setOnPageClickListener(new OnPageItemClickListener() {
                    @Override
                    public void onPageItemClick(int position) {
                        ToastUtil.showToast(getAbility(),"ImageHolderCreator中设置了点击事件，但是被 onPageItemClick 覆盖了 " + position);
                    }
                }).setOuterPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                indicatorView.invalidate();
            }
        }) .setPageMargin2(0,60)
                .setPages(list);

        add.setClickedListener(component -> {
            list.add(Utils.getRandom());
            banner.setIndicator(indicatorView
                    .setIndicatorSelectorColor(Color.WHITE));
            banner.setPageMargin(-(AttrHelper.vp2px(88,getContext())),AttrHelper.vp2px(37,getContext()));
            banner.setPages(list, banner.getCurrentPager());
            updateLoopText();
        });
        remove.setClickedListener(component -> {
            if (list.size() == 0) {
                return;
            }
            int listRandom = new SecureRandom().nextInt(list.size());
            ToastUtil.showToast(getAbility(),"删除第" + listRandom + " 张");
            list.remove(list.size()-1);
            banner.setPageMargin(-(AttrHelper.vp2px(88,getContext())),AttrHelper.vp2px(37,getContext()));
            banner.setIndicator(indicatorView
                    .setIndicatorSelectorColor(Color.WHITE));
            banner.setPages(list, banner.getCurrentPager());
            updateLoopText();
        });

        update.setClickedListener(component -> {
            final int size = list.size();
            list.clear();
            for (int i = 0; i < size; i++) {
                list.add(Utils.getRandom());
            }
            banner.setIndicator(indicatorView
                    .setIndicatorSelectorColor(Color.WHITE))
                    .setAutoPlay(false)
                    .setPageTransformer(true, new ScaleInTransformer());

            banner.setPages(list, banner.getCurrentPager());
            updateLoopText();
        });

        noLoop.setClickedListener(component -> {
            if (banner.isAutoPlay()) {
                banner.setAutoPlay(false);
                updateLoopText();
            } else {
                banner.setAutoPlay(true);
                if (!banner.isAutoPlay()) {
                    new ToastDialog(getContext())
                            .setText("轮播页数需要大于1")
                            .setAlignment(LayoutAlignment.CENTER)
                            .show();
                }
                updateLoopText();
            }
        });
        updateStyle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                new ArrayStringItemSelectDialog(getAbility(),Arrays.asList(INDICATOR_STR))
                        .setChoose(style)
                        .setOnItemClickListener((position, value) -> {
                            updateStyle.setText(value);
                            style = position;
                            indicatorView.setIndicatorStyle(style);
                        }).show();
            }
        });

    }
    private void updateLoopText() {
        if (banner.isAutoPlay()) {
            noLoop.setText("停止自动轮播");
        } else {
            noLoop.setText("开始自动轮播");
        }
    }
}
